﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Linq;
using System.Windows.Controls;
using Caliburn.Micro;
using OI.ViewModels;


namespace OI.Framework
{
    public class OIBootstrapper : Bootstrapper<IShell>
    {
        private CompositionContainer container;

        public CompositionContainer Container
        {
            get { return container; }
        }

        //static readonly ILog SimpleLogger = new SimpleLogger();

        protected override void Configure()
        {
            container = CompositionHost.Initialize(
                new AggregateCatalog(
                    AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                    )
                );

            var batch = new CompositionBatch();

            // global event aggregator
            var ea = new EventAggregator();
            batch.AddExportedValue<IEventAggregator>(ea);

            // global window manager
            var wm = new WindowManager();
            batch.AddExportedValue<IWindowManager>(wm);

            // global drill-down manager
            batch.AddExportedValue(new DrillDownManager(ea));

            // global sharing manager
            batch.AddExportedValue(new SharingManager(ea));

            // create a global instance of the map definition for the master mashup map
            batch.AddExportedValue(new MashupMapDefinition());

            // map sharing
            // create an instance now so the singleton can receive events in advance of its display
            batch.AddExportedValue(new DatasetDetailsOptionsPanelViewModel(ea, wm));
            batch.AddExportedValue(new MashupToolboxViewModel(ea, wm));
            
            batch.AddExportedValue(container);

            container.Compose(batch);

            ConventionManager.AddElementConvention<BusyIndicator>(BusyIndicator.IsBusyProperty, "IsBusy", null);
            
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = container.GetExportedValues<object>(contract);

            if (exports.Count() > 0)
                return exports.First();

            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override void BuildUp(object instance)
        {
            container.SatisfyImportsOnce(instance);
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }
    }
}
